<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller{

    public function upload(Request $request){
        try {
            if ($request->hasFile('avatar') && $request->file('avatar')->isValid()) {
                $photo = $request->file('avatar');
                $realPath = $photo->getRealPath();//临时文件路径
                $name = hash_file('md5',$realPath);
                $time = date('Y-m-d');
                $fileName = $time.'/'.$name.'.'.$photo->getClientOriginalExtension();
                $result = $photo->storeAs('avatar',$fileName);
                $return = ['path'=>asset('storage/'.$result)];
                return $this->returnJson(0,'',$return);
            }
            if ($request->hasFile('qcode') && $request->file('qcode')->isValid()) {
                $photo = $request->file('qcode');
                $realPath = $photo->getRealPath();//临时文件路径
                $name = hash_file('md5',$realPath);
                $time = date('Y-m-d');
                $fileName = $time.'/'.$name.'.'.$photo->getClientOriginalExtension();
                $result = $photo->storeAs('qcode',$fileName);
                $return = ['path'=>asset('storage/'.$result)];
                return $this->returnJson(0,'',$return);
            }
        } catch (\Exception $e) {
            return $this->returnJson(1, $e->getMessage());
        }
    }

}